//
//  ZHDropSelectedView.m
//  ZHDropDownView
//
//  Created by 正合适 on 2017/11/17.
//  Copyright © 2017年 Rainy. All rights reserved.
//

#import "ZHDropSelectedView.h"

#define topMargin _config.topMargin

@interface ZHDropSelectedView ()
/**按钮ARR*/
@property (nonatomic, strong) NSMutableArray<UIButton *> *buttonsArr;
/**配置*/
@property (nonatomic, strong) ZHDropDownConfig *config;
/**容器*/
@property (nonatomic, strong) UIView *containerView;
@end

@implementation ZHDropSelectedView
- (instancetype)initWithButtons:(NSArray<UIView *> *)buttons withConfig:(ZHDropDownConfig *)config {
    self = [super init];
    if (self) {
        _buttonsArr = buttons.mutableCopy;
        _config = config;
        [self createUI];
    }
    return self;
}

#pragma mark - 初始化界面
- (void)createUI {
    self.backgroundColor = kRGBA(0, 0, 0, 0);
    float height = [self selectViewHeight];
    self.frame = CGRectMake(_config.originalPoint.x, _config.originalPoint.y, _config.selectedViewWidth, topMargin+height);
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, height+topMargin)];
    _containerView.backgroundColor = _config.containerColor;
    _containerView.clipsToBounds = YES;
    [self addSubview:_containerView];
    [self drawShapLayer];
    [self addSubViews];
    
}

- (void)drawShapLayer {
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = [self drawPathWithStartBfb:_config.anchorPointX].CGPath;
    layer.strokeColor = [UIColor blackColor].CGColor;
    layer.fillColor = [UIColor whiteColor].CGColor;
    _containerView.layer.mask = layer;
}

#pragma mark - 画曲线
- (UIBezierPath *)drawPathWithStartBfb:(float)bfb {
    float viewHeight = _containerView.frame.size.height;
    float viewWidth = _config.selectedViewWidth;
    float x = bfb*_config.selectedViewWidth;
    // 半径
    float arc = _config.cornerRadius;
    // 三角形底边一半宽度
    float w = _config.arcW;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(bfb*_config.selectedViewWidth, 0)];
    float p1x = x > w+arc? x-w : w+arc;
    [path addLineToPoint:CGPointMake(p1x, topMargin)];
    // 左上角的圆弧
    [path addArcWithCenter:CGPointMake(arc, topMargin+arc) radius:arc startAngle:1.5*M_PI endAngle:1*M_PI clockwise:NO];
    [path addLineToPoint:CGPointMake(0, viewHeight-arc)];
    // 左下角圆弧
    [path addArcWithCenter:CGPointMake(arc, viewHeight-arc) radius:arc startAngle:1*M_PI endAngle:0.5*M_PI clockwise:NO];
    [path addLineToPoint:CGPointMake(viewWidth-arc, viewHeight)];
    // 右下角圆弧
    [path addArcWithCenter:CGPointMake(viewWidth-arc, viewHeight-arc) radius:arc startAngle:0.5*M_PI endAngle:0*M_PI clockwise:NO];
    [path addLineToPoint:CGPointMake(viewWidth, topMargin+arc)];
    // 右上角圆弧
    [path addArcWithCenter:CGPointMake(viewWidth-arc, topMargin+arc) radius:arc startAngle:2*M_PI endAngle:1.5*M_PI clockwise:NO];
    [path addLineToPoint:CGPointMake(x+w, topMargin)];
    [path closePath];
    path.lineWidth = 2;
    path.lineCapStyle = kCGLineCapRound;
    return path;
}

#pragma mark - 添加按钮
- (void)addSubViews {
    float h = topMargin;
    for (NSInteger i = 0; i < _buttonsArr.count; i ++) {
        UIView *view = _buttonsArr[i];
        view.frame = CGRectMake(0, h, view.frame.size.width, view.frame.size.height);
        [_containerView addSubview:view];
        h += view.frame.size.height+_config.optionMargin;
    }
}

#pragma mark - 获取selectview的高度
- (float)selectViewHeight {
    float heigth = 0;
    for (UIView *view in _buttonsArr) {
        heigth += view.frame.size.height+_config.optionMargin;
    }
    return heigth - _config.optionMargin;
}

#pragma mark - 展开动画
- (void)animation:(UIView *)view {
    // 修改锚点后重新设置原点，保证位置是设置的原点
    self.layer.anchorPoint = CGPointMake(_config.anchorPointX, 0);
    float x = self.frame.origin.x - (0.5-_config.anchorPointX)*self.frame.size.width;
    float y = self.frame.origin.y - 0.5* self.frame.size.height;
    self.frame = CGRectMake(x, y, self.frame.size.width, self.frame.size.height);
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = _config.animationTime;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.04, 1.04, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.96, 0.96, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [view.layer addAnimation:animation forKey:nil];
}

- (void)showWithAnimation {
    if (_config.showWithAnimation) {
        [self animation:self];
    }
}
@end
